package yxy.game.pm2.command.union.war;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionWarMessage;
import yxy.game.pm2.module.arena.ArenaHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.war.RankUnionWar;
import yxy.game.pm2.module.union.war.UnionWarPlayerModule;

import java.util.Collection;

/** 联盟战 联盟战排行榜 */
@MessageType(proto = Command.UnionWarRank, classtype = UnionWarMessage.UnionWarRankRequest.class)
public class CMD_UnionWarRank extends Command<UnionWarMessage.UnionWarRankRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionWarMessage.UnionWarRankRequest request) {
		String playerCode = messageSource.getTag();

		UnionWarMessage.UnionWarRankResponse.Builder response = UnionWarMessage.UnionWarRankResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry unionPlayerEntry = new UnionPlayerEntry(playerCode);
		{
			Result result = new UnionHandle().checkPlayerUnion(unionPlayerEntry);
			if (!result.equals(Result.UnionIn)) {
				send(messageSource, response, result);
				return;
			}
		}
		UnionBean union = unionPlayerEntry.getUnionBean();

		RankUnionWar rankArena = new RankUnionWar(union.getCode());
		try {
			Collection<RankEntry> top = rankArena.top(rankArena.topSize());
			int rank = 0;
			ArenaHandle handle = new ArenaHandle();

			for (RankEntry entry : top) {
				String pCode = entry.getKey();
				int score = entry.getValue().intValue();
				UnionWarPlayerModule unionWarPlayerModule = UnionWarPlayerModule.get(pCode);

				UnionWarMessage.RankUnionWar.Builder builder = UnionWarMessage.RankUnionWar.newBuilder();

				builder.setRank(++rank);
				builder.setPlayerInfo(handle.oppoInfo(pCode));
				builder.setScore(score);
				builder.setStar(unionWarPlayerModule.getStar());
				response.addRank(builder);
			}
			{
				int playerRank = 0;
				int score = 0;
				int star = 0;
				RankEntry entry = rankArena.get(playerCode);
				if (entry != null) {
					playerRank = rankArena.rank(playerCode);
					score = entry.getValue().intValue();
					UnionWarPlayerModule unionWarPlayerModule = UnionWarPlayerModule.get(playerCode);
					star = unionWarPlayerModule.getStar();
				}

				UnionWarMessage.RankUnionWar.Builder builder = UnionWarMessage.RankUnionWar.newBuilder();
				builder.setRank(playerRank);
				builder.setPlayerInfo(handle.oppoInfo(playerCode));
				builder.setScore(score);
				builder.setStar(star);
				response.setMy(builder);
			}

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionWarMessage.UnionWarRankResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}